function [C,flag_nochol]=var2tchol(sig,vdeg,scale); 
% ======================================================
% VAR2TCHOL 
% function [C,flag_nochol]=var2tchol(sig,vdeg,scale); 
% 
% Given the variance covariance matrix SIG 
% convert into a cholesky transpose to use with 
% MULTVTMCMC.m which uses C as an input 
% The variance covariance matrix is multiplied by SCALE
% Use this with a Student kernel in mcmc sampling and a
% scaled Metropolis proposal 
%
% See TEST_T.m for a debugging example 
% VDEG must be greater than 2 
%
% If a CHOLESKY cannot be obtained then the code
% returns C=[] and flag_nochol == 1 
% else flag_nochol == 0 
%
% This is under the Gelman et al. parametrization 
% s.t. if x~t(m,V,vdeg); 
%        Var(x)=(vdeg/(vdeg-2))*V 
%
% Alejandro Justiniano 
% Debugged 5/1/2007
% ======================================================
if vdeg < 2+eps; error('vdeg must be greater than 2'); end
if nargin < 3 | isempty(scale);
    scale=1; 
end 
V=sig*((vdeg-2)/vdeg);
[C,check]=chol(V);
if check~=0; 
    flag_nochol=1; C=[]; return; 
end 
C=(sqrt(scale)*C)';flag_nochol=0;